<template>
	<view class="quickPopupView">
		<view class="title" :style="{'height': navHeight+statusBarHeight + 'px'}">
			<text v-if="!editData.type" class="iconfont iconhuaban73" @tap="$emit('close')"></text>
			<text v-if="editData.type" class="iconhuaban73" @tap="editData.type=''" style="font-size: 28rpx;">取消</text>
			<text>快捷语言</text>
		</view>
		<view class="text-list" v-if="!editData.type">
			<view class="row" v-if="listData.loading"><view class="text">加载中...</view></view>
			<view class="row" v-if="listData.list<1 && !listData.loading"><view class="text">暂无数据</view></view>
			<view class="row" v-for="item in listData.list">
				<view class="text">{{item.content}}</view>
				<view class="btns">
					<uni-icons @tap="editData.input=item.content;editData.quick_id=item.id;editData.type='edit'" type="compose" size="26" color="#CCCCCC"></uni-icons>
					<uni-icons @tap="editData.quick_id=item.id;delEvent()" type="trash" size="26" color="#CCCCCC" style="margin-left: 20rpx;"></uni-icons>
				</view>
			</view>
		</view>
		<view class="edit-cont" v-if="editData.type">
			<textarea v-model="editData.input"/>
			<button v-if="editData.type=='edit'" @tap="editEvent">保存</button>
			<button v-if="editData.type=='add'" @tap="addEvent">添加</button>
		</view>
		<view class="footer" @tap="editData.type='add'" v-if="!editData.type">
			<text>添加</text>
		</view>
	</view>
</template>

<script>
	import customeApi from '@/service/custome.js'
	export default {
		components: {
		},
		computed: {
			navHeight() {
				return this.$store.state.system.navBarHeight
			}, // Nav栏高度
			statusBarHeight() {
				let systemInfo = uni.getSystemInfoSync()
				return systemInfo.statusBarHeight || 0
			}
		},
		data() {
			return {
				listData:{
					list:[],
					loading:false
				},
				editData:{
					type:false,
					input:'',
					quick_id:''
				}
			}
		},
		mounted() {
			this.getListData();
		},
		methods: {
			getListData(){
				this.listData.loading = true;
				customeApi.quickList({
					page:1,
					pagesize:1000
				}).then(res => {
					if(res.code == 1){
						this.listData.list = res.data.list
					}else{
						uni.showToast({
						    title: res.msg,
						    duration: 2000
						});
					}
				}).catch(err=>{
					
				}).finally(r=>{
					this.listData.loading = false;
				})
			},
			editEvent(){
				uni.showLoading({
				    title: '修改中'
				});
				customeApi.quickEdit({
					content: this.editData.input,
					quick_id: this.editData.quick_id 
				}).then(res => {
					if(res.code == 1){
						this.editData.type='';
						this.editData.input = '';
						this.getListData()
					}
					uni.showToast({
					    title: res.msg,
					    duration: 2000
					});
				}).catch(err=>{
					
				}).finally(r=>{
					uni.hideLoading()
				})
			},
			addEvent(){
				uni.showLoading({
				    title: '添加中'
				});
				customeApi.quickAdd({
					content: this.editData.input,
				}).then(res => {
					if(res.code == 1){
						this.editData.type='';
						this.editData.input = '';
						this.getListData()
					}
					uni.showToast({
					    title: res.msg,
					    duration: 2000
					});
				}).catch(err=>{
					
				}).finally(r=>{
					uni.hideLoading()
				})
			},
			delEvent(){
				uni.showModal({
				    title: '提示',
				    content: '确定删除该项?',
				    success: (con) => {
				        if (con.confirm) {
							uni.showLoading({
							    title: '删除中'
							});
							customeApi.quickDel({
								quick_id: this.editData.quick_id 
							}).then(res => {
								if(res.code == 1){
									this.getListData()
								}
								uni.showToast({
								    title: res.msg,
								    duration: 2000
								});
							}).catch(err=>{
								
							}).finally(r=>{
								uni.hideLoading()
							})
				        }
				    }
				});
			}
		}
	}
</script>

<style lang="scss">
	.quickPopupView {
		overflow: hidden;
		display: flex;
		flex-direction: column;
		width: 100%;
		height: 100%;
		background-color: #fff;

		.title {
			display: flex;
			width: 100%;
			height: 88rpx;
			background: #F6F7FB;
			align-items: center;

			.iconhuaban73 {
				font-size: 36rpx;
				color: #333333;
				padding: 0 20rpx;
			}

			.serch {
				display: flex;
				align-items: center;
				width: 422rpx;
				height: 62rpx;
				background: #FFFFFF;
				border-radius: 34rpx;

				.iconhuaban18 {
					font-size: 32rpx;
					color: #939393;
					padding: 0 20rpx 0 10rpx;
				}

			}
		}
		.text-list{
			.row{
				display: flex;
				padding: 40rpx 36rpx;
			}
			.text{
				flex: 1;
				font-size: 28rpx;
				color: #333333;
			}
			.btns{
				
			}
		}
		.footer{
			position: fixed;
			bottom: 0;
			width: 100%;
			height: 90rpx;
			line-height: 90rpx;
			text-align: center;
			background: linear-gradient(86deg, #DBA954 0%, #EABA6A 100%);
			text{
				color: #fff;
			}
		}
		
		.edit-cont{
			textarea{
				width: 686rpx;
				height: 440rpx;
				background: #F8F8F8;
				opacity: 1;
				border-radius: 8rpx;
				margin: 0rpx auto;
				margin-top: 50rpx;
				box-sizing: border-box;
				padding: 20rpx;
			}
			button{
				padding: 0;
				margin: 50rpx auto;
				width: 520rpx;
				height: 92rpx;
				background: #DBA954;
				opacity: 1;
				border-radius: 46rpx;
				color: #fff;
			}
		}
	}
</style>
